package com.ghovos.usermangerpgdemo.contorller.api;

import com.ghovos.usermangerpgdemo.common.result.GlobalResult;
import com.ghovos.usermangerpgdemo.entity.DepartmentEntity;
import com.ghovos.usermangerpgdemo.service.DepartmentService;
import com.ghovos.usermangerpgdemo.vo.req.DepartUserListReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author EdgarLiu(刘凌峰) <br/>
 * date: 2022/3/18 <br/>
 * time: 上午11:06 <br/>
 * email: EdgarLlf@foxmail.com
 */
@RestController
@AllArgsConstructor
@RequestMapping("/department")
@Api(tags = "部门管理")
public class DepartmentController {
    private final DepartmentService departmentService;

    @ApiOperation("所有部门")
    @GetMapping("/all")
    public GlobalResult<List<DepartmentEntity>> all(){
        return GlobalResult.ok(departmentService.list());
    }

    @ApiOperation("所有部门， 包括用户信息")
    @GetMapping("/all/ext")
    public GlobalResult<List<DepartmentEntity>> allExt(){
        return GlobalResult.ok(departmentService.allDepartmentListExt());
    }

    @ApiOperation("添加部门")
    @PostMapping("/add")
    public GlobalResult<Object> addDepartment(@RequestBody DepartmentEntity departmentEntity) {
        return GlobalResult.ok(departmentService.save(departmentEntity));
    }

    @ApiOperation("根据id更新部门")
    @PostMapping("/update")
    public GlobalResult<Object> updateDepartment(@RequestBody DepartmentEntity departmentEntity) {
        return GlobalResult.ok(departmentService.updateById(departmentEntity));
    }


    @ApiOperation("根据id删除部门， 并把用户的id置为null")
    @GetMapping("/delete/{id}")
    public GlobalResult<Object> deleteWithUser(@PathVariable("id") Long id) {
        return GlobalResult.ok(departmentService.deleteByIdWithUser(id));
    }

    @ApiOperation("根据部门id 批量添加用户")
    @PostMapping("/add/users")
    public GlobalResult<Object> addUserByIdList(@RequestBody DepartUserListReq departUserListReq) {
        return GlobalResult.ok(departmentService.addUserByIds(departUserListReq.getDepartmentId(), departUserListReq.getUserIdList()));
    }

    @ApiOperation("批量删除用户")
    @PostMapping("/delete/users")
    public GlobalResult<Object> deleteUserByIdList(@RequestBody DepartUserListReq departUserListReq) {
        return GlobalResult.ok(departmentService.deleteUserByIds(
                departUserListReq.getDepartmentId(), departUserListReq.getUserIdList()
        ));
    }

    @ApiOperation("根据部门id查询部门")
    @GetMapping("/find/{id}")
    public GlobalResult<Object> getDepartmentById(@PathVariable("id") Long id) {
        return GlobalResult.ok(departmentService.getById(id));
    }

    @ApiOperation("根据部门id查询部门 包括额外信息")
    @GetMapping("/find/ext/{id}")
    public GlobalResult<Object> getDepartmentExtById(@PathVariable("id") Long id) {
        return GlobalResult.ok(departmentService.getExtById(id));
    }
}
